package cn.fl.infside.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.infside.service.IInfWarrantProjectService;
import cn.fl.infside.transform.InfWarrantProjectVOTransform;
import cn.fl.infside.vo.InfWarrantProjectVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import fl.inf.facade.api.IWarrantApiService;
import fl.inf.facade.dto.InfWarrantProjectDTO;
import fl.inf.facade.dto.MessageDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 权证系统项目接口表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class InfWarrantProjectServiceImpl implements IInfWarrantProjectService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IInfWarrantProjectService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

	@Autowired
	private IWarrantApiService warrantApiService;
	/**
	 * 组织架构 API 暴露服务
	 */
	@Autowired
	private ISysOrgApiService sysOrgApiService;

	/**
	 * CstBscInfo 服务层BEAN
	 */
	@Autowired
	private ICstBscInfoService cstBscInfoService;


	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public InfWarrantProjectVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		InfWarrantProjectDTO dto = null;//this.自定义ApiService.selectById(id);
		return InfWarrantProjectVOTransform.toVO(dto);
	}

	@Override
	public Long add(InfWarrantProjectVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.infWarrantProjectApiService.add(InfWarrantProjectVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.infWarrantProjectApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(InfWarrantProjectVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.infWarrantProjectApiService.modify(InfWarrantProjectVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.warrantApiService.findProjectsByPager(page);
	}
	@Override
	public JSONArray searchOrgTreeByCurrentUser(String currentBuOrgCd, String currentSysCd) throws BaseException {
		JSONArray jsonArray = new JSONArray ();
		SysOrgDTO sysOrgDto  = null;
		if(StringUtils.isNotBlank(currentBuOrgCd)){
			List<SysOrgDTO> orgList  = null;
			sysOrgDto = 	this.sysOrgApiService.selectSysOrgByOrgCd(currentBuOrgCd,currentSysCd);
			List<Long> orgId = new ArrayList<Long>();
			orgId.add(sysOrgDto.getId());
			if(null!=sysOrgDto){
				orgList = this.sysOrgApiService.searchRecuOrgWithchildBy(orgId, currentSysCd);
			}
			if(CollectionUtils.isEmpty(orgList)){
				return jsonArray;
			}
			for (SysOrgDTO sysOrgDTO : orgList) {
				JSONObject obj = new JSONObject();
				obj.put("ischecked", false);
				obj.put("id", sysOrgDTO.getId());
				obj.put("text", sysOrgDTO.getOrgNm());
				obj.put("pid", sysOrgDTO.getOrgPid());
				obj.put("orgCd", sysOrgDTO.getOrgCd());
				obj.put("orgDispNm", sysOrgDTO.getOrgDispNm());
				jsonArray.add(obj);
			}
		}
		JSONArray treeList = this.treeList(jsonArray,sysOrgDto.getOrgPid());
		return treeList;
	}
	/**
	 * 递归添加整个集合数据
	 * @param treeList
	 * @param parentId
	 * @return
	 */
	private JSONArray treeList(JSONArray treeList, Long parentId) {
		JSONArray childMenu = new JSONArray();
		for (Object object : treeList) {
			JSONObject jsonMenu = JSONObject.parseObject(JSON.toJSONString(object));
			long oid = jsonMenu.getLong("id");
			long pid = jsonMenu.getLong("pid");
			if (parentId == pid) {
				JSONArray c_node = treeList(treeList, oid);
				jsonMenu.put("children", c_node);
				childMenu.add(jsonMenu);
			}
		}
		return childMenu;
	}

	@Override
	public List<KeyValueVO> getKvList(String codeGrpCd, String sysCd, String orgCd) throws BaseException {
		return this.sysDictionaryApiService.getKvListAll(codeGrpCd,sysCd,orgCd);
	}

	@Override
	public String searchOrgNm(String currentBuOrgCd, String currentSysCd) throws BaseException {
		String orgnm = "";
		if(StringUtils.isNotBlank(currentBuOrgCd)){
			SysOrgDTO sysOrgDto = 	this.sysOrgApiService.selectSysOrgByOrgCd(currentBuOrgCd,currentSysCd);
			if(sysOrgDto != null){
				orgnm = sysOrgDto.getOrgDispNm();
			}
		}
		return orgnm;
	}

	@Override
	public CstBscInfoVO searchCstBscInfoDetail(Long cstid) throws BaseException {
		CstBscInfoVO vo = new CstBscInfoVO();
		if(cstid == null) return vo;
		vo = this.cstBscInfoService.selectDetailById(cstid);
		return vo;
	}

	@Override
	public Map<String, String> searchGroupToMap(String sysCd, String orgCd, String codeGrpCd) throws BaseException{
		return this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, codeGrpCd);
	}

	@Override
	public MessageDTO<String> projectApply(Long projectId) {
		MessageDTO<String> messagedto = this.warrantApiService.projectApply(projectId);
		return messagedto;
	}
}
